// Written by Dale J Williams.
// Licensed under MPL 1.1 unless otherwise specified. 
// http://www.mozilla.org/MPL/MPL-1.1.html

package engineDJW {
	
	import engineDJW.input.KeyboardManager;
	import engineDJW.load.LoadManager;
	import engineDJW.screens.ScreenManager;
	import engineDJW.sound.SoundManager;
	import engineDJW.tween.TweenManager;
	import flash.display.Sprite;
	import flash.display.Stage;
	/**
	 * ...
	 * @author Dale J Williams
	 */
	public class GameCore {
		
		static public const VERSION		:String		= "v1.0a";
		
		static private var _instance	:GameCore;
		
		private var _settingsURL:String;
		
		private var _loader		:LoadManager 		= new LoadManager();
		private var _tween		:TweenManager		= new TweenManager();
		private var _input		:KeyboardManager;
		private var _screen		:ScreenManager;
		private var _sound		:SoundManager;
		private var _engine		:GameEngine;
		private var _stage		:Stage;
		private var _cont		:Sprite;
		private var _overlay	:Sprite;
		private var _settings	:XML;
		private var _path		:String;
		private var _complete	:Boolean;
		
		private var _onComplete	:Function;
		
		public function GameCore($stage:Stage, $settingsURL:String=null, $path:String = null) {
			trace("EngineDJW " + VERSION);
			
			_instance = this;
			_stage = $stage;
			_path = $path || "";
			Main.self.addChild(_cont = new Sprite());
			Main.self.addChild(_overlay = new Sprite());
			
			_input = new KeyboardManager(_stage);
			_screen = new ScreenManager(_cont);
			_sound = new SoundManager();
			
			if ($settingsURL) load($settingsURL);
			else loadComplete();
		}
		
		public function load($settingsURL:String = null):void {
			_settingsURL = $settingsURL || _settingsURL;
			_loader.addToCue(_settingsURL, "settingsXML");
			_loader.onComplete = loadAssets;
			_loader.load();
		}
		
		private function loadAssets():void {
			_settings = XML(_loader.getData("settingsXML"))
			for each(var node:XML in _settings..@file) {
				node = node.parent();
				_loader.addToCue(_path + node.@file, node.@name);
			}
			_loader.onComplete = loadComplete;
			_loader.load();
		}
		
		private function loadComplete():void {
			if (_onComplete != null) _onComplete();
		}
		
		public function get stage():Stage { return _stage; }
		
		public function get path():String { return _path; }
		
		public function get complete():Boolean { return _complete; }
		
		public function get settings():XML { return _settings; }
		
		public function get onComplete():Function { return _onComplete; }		
		public function set onComplete(value:Function):void {
			_onComplete = value;
		}
		
		public function get loader():LoadManager { return _loader; }
		
		public function get screen():ScreenManager { return _screen; }
		
		public function get input():KeyboardManager { return _input; }
		
		public function get width():Number {
			return _stage.stageWidth;
		}
		
		public function get height():Number {
			return _stage.stageHeight;
		}
		
		public function get tween():TweenManager { return _tween; }

		public function get engine():GameEngine { return _engine; }		
		public function set engine(value:GameEngine):void {
			_engine = value;
		}
		
		public function get sound():SoundManager { return _sound; }
		
		public function get overlay():Sprite { return _overlay; }
		
		static public function get instance():GameCore { return _instance; }
		
	}

}